<?php
class RegisterAction extends Action {
    public function index()
	{
        $this->display("Register");
    }

	Public function verify()
	{
		import('ORG.Util.Image');
		Image::buildImageVerify();
	}
	public function Regist()
	{
		
		if($_SESSION['verify'] != md5($_POST['verify'])) 
		{
			$this->error('验证码错误！');
		}

		$UserName = $_POST['UserName'];
		$Password = $_POST['Password'];
		$ConfirmPassword = $_POST['ConfirmPassword'];
		$Email = $_POST['Email'];
		$User = D("Users");

		$UserInfo = $User->where("UserName ='$UserName'")->find();

		if(empty($UserInfo))
		{
			 
			$Model = new Model();
			$sqlB = new StringBuilder();
			$sqlB->Append("INSERT INTO Users ");
			$sqlB->Append(" ( ");
			$sqlB->Append("   UserName ");
			$sqlB->Append(" , Password ");
			$sqlB->Append(" , Email ");
			$sqlB->Append(" , Role ");
			$sqlB->Append(" , TimeUpdated ");
			$sqlB->Append(" , UpdatedBy ");
			$sqlB->Append(" , TimeCreated ");
			$sqlB->Append(" , CreatedBy ");
			$sqlB->Append("   ) ");
			$sqlB->Append("SELECT ");
			$sqlB->Append("   " . FData($UserName, "Text") . " AS UserName ");
			$sqlB->Append(" , " . FData($Password, "Text") . " AS Password ");
			$sqlB->Append(" , " . FData($Email, "Text") . " AS Email ");
			$sqlB->Append(" , 2 AS Role ");
			$sqlB->Append(" , NOW() AS TimeUpdated ");
			$sqlB->Append(" , ". FData($UserName, "Text") . " AS UpdatedBy ");
			$sqlB->Append(" , NOW() AS TimeCreated ");
			$sqlB->Append(" , ". FData($UserName, "Text") . " AS CreatedBy ");
			$sqlB->Append(";");

			$result = $Model->execute($sqlB->ToString());

			if($result != ""){
					$this->success('注册成功!',"__APP__/Login/index");
				}
				else{
					$this->success('注册失败!',"__APP__/Register/index");
				}
		}
		else{
			$this->error('注册失败,已经存在此用户!', "__APP__/Register/index");
		}
	}
	Public function VerifyUser()
	{
		$User = D("Users");
		$UserName = $_GET['UserName'];
		$Email = $_GET['Email'];

		if($UserName != ""){
			$UserInfo = $User->where("UserName ='$UserName'")->find();
		}

		if($Email != ""){
			$UserInfo = $User->where("Email ='$Email'")->find();
		}

		if(!empty($UserInfo)){
			echo "True";
		}
		else{
			echo "False";
		}
	}

}
